package com.meichis.mcsappframework.dialog;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.BitmapDrawable;
import android.view.*;
import android.widget.*;

import com.meichis.mcsappframework.R;
import com.meichis.mcsappframework.utils.ScreenUtil;

import java.lang.reflect.Method;
import java.util.List;

public class DropDownPopupWindow<T> extends PopupWindow {
    private Context mContext;
    private  List<T> mList = null;

    private ListView listView;

    private String TextData;
    ItemAddAdapter adapter;
    private int mResid, mTextGravity;

    public interface SelectedListener {

        void setSelect(int position);
    }

    private SelectedListener selectedListener;

    public DropDownPopupWindow(Context context, String dTextData, List<T> list, SelectedListener selectlistener) {
        this(context, R.drawable.bg_dropdown_light, dTextData, list, selectlistener);
    }

    public DropDownPopupWindow(Context context, int resid, String dTextData, List<T> list, SelectedListener selectlistener) {
        this(context, resid, Gravity.LEFT, dTextData, list, selectlistener);
    }

    public DropDownPopupWindow(Context context, int resid, int textGravity, String dTextData, List<T> list, SelectedListener selectlistener) {
        this(context, resid, textGravity, ScreenUtil.getScreenWidth(), WindowManager.LayoutParams.WRAP_CONTENT, dTextData, list, selectlistener);
    }

    public DropDownPopupWindow(Context context, int resid, int textGravity, int width, int height, String dTextData,  List<T> list, SelectedListener selectlistener) {
        super(context);
        mContext = context;
        mResid = resid;
        mTextGravity = textGravity;
        mList = list;
        TextData = dTextData;
        this.selectedListener = selectlistener;
        View view = getView(context);
        view.setBackgroundResource(resid);
        setContentView(view);
        setWidth(width);
        setHeight(height);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
    }


    private View getView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_pop_choice, null);
        try {
            this.dismiss();
        } catch (Exception e) {
        }


        listView = (ListView) view.findViewById(R.id.lst);
        adapter = new ItemAddAdapter();
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                selectedListener.setSelect(position);
                DropDownPopupWindow.this.dismiss();
            }
        });
        return view;
    }

    public void notifyDataChanged() {
        adapter.notifyDataSetChanged();
    }

    public class ItemAddAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (mList != null) {
                return mList.size();
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            if (mList != null) {
                return mList.get(position);
            } else {
                return null;
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String item = "";
            if (mList.get(position) instanceof CharSequence) {//字符串
                item = mList.get(position).toString();
            } else {//实体类
                Class<? extends Object> objClazz = mList.get(position).getClass();
                Method fieldGetMet;
                try {
                    fieldGetMet = objClazz.getMethod("get" + TextData, new Class[]{});

                    Object fieldVal = fieldGetMet.invoke(mList.get(position), new Object[]{});
                    if (fieldVal != null) {
                        item = String.valueOf(fieldVal);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            TextView textView = null;
            if (convertView == null) {
                textView = new TextView(mContext);
                textView.setPadding(20, 20, 20, 20);
                textView.setTextSize(18);
                textView.setGravity(mTextGravity);
                textView.setSingleLine(true);
                ColorStateList cls = null;
                if (mResid == R.drawable.bg_dropdown_dark) {
                    cls = (ColorStateList) mContext.getResources().getColorStateList(R.color.selector_popview_dark);
                } else {
                    cls = (ColorStateList) mContext.getResources().getColorStateList(R.color.selector_popview_light);
                }
                textView.setTextColor(cls);
            } else {
                textView = (TextView) convertView;
            }
            textView.setText(item);
            return textView;
        }

    }
}
